Line Graph

This notebook demonstrates how to create a simple line graph using Python’s matplotlib. We’ll start with sample data and then show you how to load and visualize your own dataset.

1. Basic Line Graph

# Step 1: Import Libraries
import matplotlib.pyplot as plt
import numpy as np

# Step 2: Create Sample Data
x = np.linspace(0, 23, 2*24)
np.random.seed(42)
noise = np.random.uniform(-1, 1.5, size=x.shape)
y = 20 + 8 * np.sin((x - 9) * np.pi / 12) + noise

# Step 3: Plotting the Basic Line Graph
plt.figure(figsize=(10, 6))
plt.plot(x, y, label='sin(x)', color='blue', linewidth=1)
plt.title('Basic Line Graph')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.ylim(10,30)
plt.grid(True)
plt.show()
../../_images/a1d9c0e9f72f8b0f52f35d7546ff16741db9e6b8a1c4c5990c27f4a2543224cc.png

2. Line Graph using CDS Dataset

1. Using Matplotlib

import netCDF4 as nc
from utils.compute_linegraph import compute_df_stats, plot_linegraph, plot_linegraph_with_slider

# Read netCDF file and variable data
filepath = r"./data/reanalysis-era5-land-monthly-means_2m_temperature_1950_2024.nc"
dataset = nc.Dataset(filepath, mode='r')
test_variable = 't2m'
variable_data = dataset[test_variable]

# Compute statistics
df_statistics = compute_df_stats(variable_data)

# Plot line graph for month=August (8)
plot_linegraph(selected_month=8, df_statistics=df_statistics)
../../_images/05b414677a445d1c94c4bd096669b1c30b1b8db5468e50a346b85a1af682360d.png

2. Using Plotly

# Plot line graph with month slider
plot_linegraph_with_slider(df_statistics=df_statistics)